/*
 * To change this template, choose Tools | Templates
 * AND open the template in the editor.
 */

package modelos;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author usuario
 */
public class ModeloCategoria extends ModeloDB{

    private int id;
    private String descripcion,status;

    public ModeloCategoria(int id, String descripcion, String status) {
        super();
        this.id = id;
        this.descripcion = descripcion;
        this.status = status;
    }

    public ModeloCategoria() {
        super();
    }

    public boolean registar(){
        conectar();
      
        String sql="INSERT INTO Categoria (id,descripcion,status ) VALUES(?,?,?)";
        PreparedStatement stam = crearPreparedStatement(sql);
        try {
            stam.setInt(1, getId());
            stam.setString(2, getDescripcion().toUpperCase());
            stam.setString(3, getStatus());
        } catch (SQLException ex) {
            Logger.getLogger(ModeloCategoria.class.getName()).log(Level.SEVERE, null, ex);
        }
        boolean sw= ejecutarQueryPrepare(stam);
        desconectar();
        System.out.println(sw);
        return sw;
    }



 public boolean  consultar(){
    conectar();
    String sql="SELECT * FROM Categoria WHERE id=? AND status='A'";
    PreparedStatement stam = crearPreparedStatement(sql);
    try {
        stam.setInt(1, getId());
    } catch (SQLException ex) {
            Logger.getLogger(ModeloCategoria.class.getName()).log(Level.SEVERE, null, ex);
     }
    boolean sw = false;
    ResultSet rs= ejecutarQuery(stam);
    try{
        if (rs.next()){
            sw = true;
            setDescripcion(rs.getString("descripcion"));
            setId(rs.getInt("id"));
            setStatus(rs.getString("status"));
         }
    } catch (SQLException ex) {
        Logger.getLogger(ModeloCategoria.class.getName()).log(Level.SEVERE, null, ex);
    }
     
    return sw;
  }


 public boolean eliminar(){
     conectar();
     String sql="UPDATE Categoria SET status = 'E' WHERE id=?  AND status = 'A'";
     PreparedStatement stam = crearPreparedStatement(sql);
     try {
         stam.setInt(1, getId());
     } catch (SQLException ex) {
            Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     boolean sw = ejecutarQueryPrepare(stam);
     desconectar();
     return sw;
 }

    public Vector<ModeloCategoria> listar(){
        Vector<ModeloCategoria> v = new Vector<ModeloCategoria>();
        String sql="SELECT * FROM Categoria WHERE status='A'";
        conectar();
        ResultSet rs= crearQuery(sql);
        try{
            while (rs.next()){
                ModeloCategoria  i = new ModeloCategoria();
                i.setDescripcion(rs.getString("descripcion"));
                i.setId(rs.getInt("id"));
                i.setStatus(rs.getString("status"));
                v.add(i);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ModeloCategoria.class.getName()).log(Level.SEVERE, null, ex);
        }
        return v;
  }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    

}
